# -*- coding: utf-8 -*-
import logging

class WaveEntry:
    def __init__(self, wave_id, title, tags):
        self.wave_id = wave_id
        self.title = title
        self.tags = tags
        
    def get_path(self, level_dict, extended_tag_dict): 
        """
        Arguments: level_list_dict: {0: {'name': <>, 'tags': tuple() }, 2: {}, 3: {}}
        Returns: [[0,2,3], [1,2,3], etc]
        """
        path_list = []
        path_list.append( [None for i in level_dict] )
        if self.tags:
            lower_tags = [tt.lower() for tt in self.tags]
            new_wave_tags = set(lower_tags)
            for tag in lower_tags:
                if extended_tag_dict.has_key(tag):
                    new_wave_tags = new_wave_tags.union(extended_tag_dict[tag])
            for i in range(len(level_dict)):
                level_tag_list = [tt.lower() for tt in level_dict[i]['tags']]
                only_one_tag = True 
                cur_length = len(path_list)
                for tag in new_wave_tags:
                    if tag in level_tag_list:
                        if only_one_tag:
                            for path in path_list:
                                path[i] = level_tag_list.index(tag)
                            only_one_tag = False
                        else:                        
                            for j in range(cur_length):
                                path_list.append(path_list[j][:])
                                path_list[-1][i] = level_tag_list.index(tag)
        return path_list
    
    def get_catalog_list(self, catalog_tag_list):
        """
        Arguments: catalog_tag_list: {0: "tags": tuple(catalog_tags)}
        Returns: set of indexes where at least one of wave tags presents 
        or tags of catalog is None (general catalog)   
        """ 
        cat = set() 
        if self.tags:
            lower_tags = [tt.lower() for tt in self.tags]
        else:
            lower_tags = []     
        for i in catalog_tag_list.keys():
            if catalog_tag_list[i]['tags']:
                cat_lower_tag = [tt.lower() for tt in catalog_tag_list[i]['tags']]
                for tag in lower_tags:
                    if tag in cat_lower_tag:
                        cat.add(i)
                        break
            else:
                cat.add(i)
        return cat
    
    def __unicode__(self):
        return self.title + ': ' + self.wave_id